package cn.hwh.springframework.beans.factory.support;

import cn.hutool.core.bean.BeanUtil;
import cn.hwh.springframework.beans.PropertyValue;
import cn.hwh.springframework.beans.PropertyValues;
import cn.hwh.springframework.beans.factory.config.BeanDefinition;
import cn.hwh.springframework.beans.factory.config.BeanReference;

import java.lang.reflect.Constructor;

/**
 * @author wenhao hu
 * @title: AbstractAutowireCapableBeanFactory
 * @projectName mini-spring
 * @description: TODO
 * @date 2022/1/2411:47
 */
public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFactory{

    /**
     * 使用CGlib的实例化方式
     */
    private InstantiationStrategy instantiationStrategy = new SimpleInstantiationStrategy();


    @Override
    protected Object createBean(String beanName, BeanDefinition beanDefinition,Object ...args) {
        Object bean;
        try {
            bean = createBeanInstance(beanName,beanDefinition,args);
            //为Bean填充属性
            applyPropertyValues(beanName,bean,beanDefinition);
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException("创建bean时出现异常");
        }
        addSingleton(beanName,bean);
        return bean;
    }

    private void applyPropertyValues(String beanName, Object bean, BeanDefinition beanDefinition) {
        try {
            PropertyValues propertyValues = beanDefinition.getPropertyValues();
            for (PropertyValue propertyValue : propertyValues.getPropertyValues()) {
                String name = propertyValue.getName();
                Object value = propertyValue.getValue();
                if (value instanceof BeanReference){
                    //依赖注入
                    BeanReference beanReference = (BeanReference) value;
                    value = getBean(beanReference.getBeanName());
                }
                //属性填充
                BeanUtil.setFieldValue(bean,name,value);
            }
        }catch (Exception e){
            throw new RuntimeException("Error setting property values :"+beanName);
        }
    }

    /**
     * 创建构造方法
     * @param beanName
     * @param beanDefinition
     * @param objects
     * @return
     */
    protected Object createBeanInstance(String beanName, BeanDefinition beanDefinition, Object[] objects){
        Constructor constructorToUse = null;
        //获取容器内实例的信息
        Class beanClass = beanDefinition.getBeanClass();
        //获取容器内实例的全部构造方法
        Constructor[] declaredConstructors = beanClass.getDeclaredConstructors();
        //循环构造方法，找到一个参数个数和objects相同的构造方法
        for (Constructor ctor : declaredConstructors) {
            //这里只是简单比较一下参数个数，实际上要考虑参数类型等问题
            if (null != objects && ctor.getParameterTypes().length == objects.length) {
                //将该构造方法保存
                constructorToUse = ctor;
                break;
            }
        }
        //创建构造方法
        return instantiationStrategy.instantiate(beanDefinition,beanName,constructorToUse,objects);
    }

    public InstantiationStrategy getInstantiationStrategy() {
        return instantiationStrategy;
    }

    public void setInstantiationStrategy(InstantiationStrategy instantiationStrategy) {
        this.instantiationStrategy = instantiationStrategy;
    }
}
